 //
//  LoginView.m
//  hnGovernment
//
//  Created by 安仔 on 17/3/3.
//  Copyright © 2017年 安仔. All rights reserved.
//

#import "LoginView.h"
#import "RegularRestrictions.h"
#import "DDTextField.h"

@interface LoginView ()<UITextFieldDelegate,GXJtextFieldDelegate,DDtextfieldDelegate>

@property (nonatomic, strong) UIImageView *signImg;
@property (nonatomic, strong) UIView *lineView;

@end



@implementation LoginView

- (instancetype) initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createLoginView];
    }
    return self;
}

- (void)createLoginView {
    
    
    self.signImg = [[UIImageView alloc] init];
    self.signImg.clipsToBounds = YES;
    self.signImg.image = [UIImage imageNamed:@"河南政务logo"];
    self.signImg.backgroundColor = [UIColor clearColor];
    [self addSubview:self.signImg];
    [self.signImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX);
        make.top.mas_equalTo(76 * m6Scale);
        make.size.mas_equalTo(CGSizeMake(300 * m6Scale, 80 * m6Scale));
    }];
    
    
    self.leftBtn = [[UIButton alloc] init];
    [self.leftBtn setTitle:@"个人用户" forState:0];
    [self.leftBtn setTitleColor:APPColor forState:UIControlStateNormal];
    self.leftBtn.titleLabel.font = [UIFont systemFontOfSize:36 * m6Scale];
    [self addSubview:self.leftBtn];
    [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(252 * m6Scale);
        make.centerX.mas_equalTo(-KScreenWidth / 4);
        make.size.mas_equalTo(CGSizeMake(160 * m6Scale, 36 * m6Scale));
    }];
    
    self.leftLine = [[UIView alloc] init];
    self.leftLine.backgroundColor = APPColor;
    [self addSubview:self.leftLine];
    [self.leftLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.leftBtn.mas_centerX);
        make.top.mas_equalTo(self.leftBtn.mas_bottom).offset(20 * m6Scale);
        make.size.mas_equalTo(CGSizeMake(160 * m6Scale, 6 * m6Scale));
    }];
    
    self.rightBtn = [[UIButton alloc] init];
    [self.rightBtn setTitle:@"法人用户" forState:0];
    [self.rightBtn setTitleColor:RGB(103, 103, 103) forState:UIControlStateNormal];
    self.rightBtn.titleLabel.font = [UIFont systemFontOfSize:36 * m6Scale];
    [self addSubview:self.rightBtn];
    [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(252 * m6Scale);
        make.centerX.mas_equalTo(KScreenWidth / 4);
        make.size.mas_equalTo(CGSizeMake(160 * m6Scale, 36 * m6Scale));
    }];
    
    
    self.rightLine = [[UIView alloc] init];
    self.rightLine.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.rightLine];
    [self.rightLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.rightBtn.mas_centerX);
        make.top.mas_equalTo(self.rightBtn.mas_bottom).offset(20 * m6Scale);
        make.size.mas_equalTo(CGSizeMake(160 * m6Scale, 6 * m6Scale));
    }];
    
    
//    self.phoneTF = [[GXJTextField alloc] initWithFrame:CGRectMake(74 * m6Scale, 375 * m6Scale, KScreenWidth - 148 * m6Scale, 80 * m6Scale)];
//    self.phoneTF.placeholder = NSLocalString(@"请输入手机号、身份证、用户名");
//    [self.phoneTF setFont:28 * m6Scale];
//    self.phoneTF.backgroundColor = RGB(243, 243, 243);
//    self.phoneTF.layer.borderWidth = 1.0;
//    self.phoneTF.layer.borderColor = RGB(203, 203, 203).CGColor;
//    self.phoneTF.cornerRadius = 6.0 * m6Scale;
//    self.phoneTF.clear = YES;
//    self.phoneTF.delegate = self;
//    [self addSubview:self.phoneTF];

    
    
    self.phoneTF = [DDTextField ddTextFieldWithPlaceHolder:@"请输入手机号" Font:28 * m6Scale textColor:[UIColor blackColor] contentInset:UIEdgeInsetsMake(0, 8, 0, 0) textChangingAction:nil];
    self.phoneTF.backgroundColor = RGB(243, 243, 243);
    self.phoneTF.layer.borderColor = RGB(203, 203, 203).CGColor;
    self.phoneTF.layer.cornerRadius = 6.0 * m6Scale;
    self.phoneTF.layer.borderWidth = 1;
    self.phoneTF.showClearButton = YES;
    self.phoneTF.delegate = self;
    [self addSubview:self.phoneTF];
    self.phoneTF.frame = CGRectMake(74 * m6Scale, 375 * m6Scale, KScreenWidth - 148 * m6Scale, 80 * m6Scale);
    
    self.pswTF = [DDTextField ddTextFieldWithPlaceHolder:@"请输入密码" Font:28 * m6Scale textColor:[UIColor blackColor] contentInset:UIEdgeInsetsMake(0, 8, 0, 15) textChangingAction:nil];
    self.pswTF.backgroundColor = RGB(243, 243, 243);
    self.pswTF.layer.borderColor = RGB(203, 203, 203).CGColor;
    self.pswTF.layer.cornerRadius = 6.0 * m6Scale;
    self.pswTF.layer.borderWidth = 1;
    self.pswTF.showClearButton = YES;
    self.pswTF.delegate = self;
    self.pswTF.isNeedChangeSecureTextEntry = YES;
    self.pswTF.proclaimedImageName = @"显示密码";
    self.pswTF.cryptographImageName = @"隐藏密码02";
    [self addSubview:self.pswTF];
    self.pswTF.frame = CGRectMake(74 * m6Scale, 535 * m6Scale, KScreenWidth - 148 * m6Scale, 80 * m6Scale);
    
    
    self.sureLoginBtn = [[UIButton alloc] init];
    [self.sureLoginBtn setTitle:NSLocalString(@"登录") forState:0];
    self.sureLoginBtn.titleLabel.font = [UIFont systemFontOfSize:36 * m6Scale];
    [self.sureLoginBtn setTitleColor:[UIColor colorWithWhite:1.0 alpha:0.95] forState:0];
    self.sureLoginBtn.backgroundColor = RGB(210, 235, 255);
    self.sureLoginBtn.layer.cornerRadius = 10.0 * m6Scale;
    self.sureLoginBtn.enabled = NO;
    [self.sureLoginBtn addTarget:self action:@selector(clickSureLoginBtn) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.sureLoginBtn];
    [self.sureLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX);
        make.top.mas_equalTo(self.pswTF.mas_bottom).offset(80 * m6Scale);
        make.size.mas_equalTo(CGSizeMake(600 * m6Scale, 80 * m6Scale));
    }];
    
    self.forgetPswBtn = [[UIButton alloc] init];
    [self.forgetPswBtn setTitleColor:RGB(203, 203, 203) forState:0];
    [self.forgetPswBtn setTitle:NSLocalString(@"忘记密码？") forState:0];
    self.forgetPswBtn.titleLabel.font = [UIFont systemFontOfSize:32 * m6Scale];
    [self.forgetPswBtn addTarget:self action:@selector(clickForgetPswBtn) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.forgetPswBtn];
    [self.forgetPswBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.sureLoginBtn.mas_right).offset(20 * m6Scale);
        make.top.mas_equalTo(self.sureLoginBtn.mas_bottom).offset(40 * m6Scale);
        make.size.mas_equalTo(CGSizeMake(170 * m6Scale, 36 * m6Scale));
    }];
    
    self.lineView = [[UIView alloc] init];
    self.lineView.backgroundColor = RGB(243, 243, 243);
    [self addSubview:self.lineView];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX);
        make.top.mas_equalTo(905 * m6Scale);
        make.size.mas_equalTo(CGSizeMake(600 * m6Scale, 2* m6Scale));
    }];
    
    self.registerBtn = [[UIButton alloc] init];
    [self.registerBtn setTitle:NSLocalString(@"立即注册") forState:0];
    self.registerBtn.titleLabel.font = [UIFont systemFontOfSize:34 * m6Scale];
    [self.registerBtn setTitleColor:APPColor forState:0];
    self.registerBtn.layer.borderColor = APPColor.CGColor;
    self.registerBtn.layer.borderWidth = 2 * m6Scale;
    self.registerBtn.layer.cornerRadius = 6 * m6Scale;
    [self.registerBtn addTarget:self action:@selector(clickRegisterBtn) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.registerBtn];
    [self.registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX);
        make.top.mas_equalTo(1006 * m6Scale);
        make.size.mas_equalTo(CGSizeMake(280 * m6Scale, 80 * m6Scale));
    }];
    
}

//- (void)GXJtextFieldChangeText:(GXJTextField *)textField {
//    if ([RegularRestrictions IsPhoneNumber:self.phoneTF.text] && self.pswTF.text.length > 0) {
//        self.sureLoginBtn.enabled = YES;
//        self.sureLoginBtn.backgroundColor = APPColor;
//    }
//    
//    if (![RegularRestrictions IsPhoneNumber:self.phoneTF.text] && self.pswTF.text.length == 0) {
//        self.sureLoginBtn.enabled = NO;
//        self.sureLoginBtn.backgroundColor = APPbuttonDisableColor;
//    }
//}

- (void)ddtextFieldChangingText:(DDTextField *)textField {
    if ([RegularRestrictions IsPhoneNumber:self.phoneTF.text] && self.pswTF.text.length > 0) {
        self.sureLoginBtn.enabled = YES;
        self.sureLoginBtn.backgroundColor = APPColor;
    }
    
    if (![RegularRestrictions IsPhoneNumber:self.phoneTF.text] && self.pswTF.text.length == 0) {
        self.sureLoginBtn.enabled = NO;
        self.sureLoginBtn.backgroundColor = APPbuttonDisableColor;
    }
}

- (void)clickSureLoginBtn {
    if ([self.delegate respondsToSelector:@selector(clickSureLoginBtn)]) {
        [self.delegate clickSureLoginBtn];
    }
}

- (void)clickForgetPswBtn {
    if ([self.delegate respondsToSelector:@selector(clickForgetPswBtn)]) {
        [self.delegate clickForgetPswBtn];
    }

}

- (void)clickRegisterBtn {
    if ([self.delegate respondsToSelector:@selector(clickRegisterBtn)]) {
        [self.delegate clickRegisterBtn];
    }
}

- (void)hideOrShowPassword {
    
}

-(CGRect)placeholderRectForBounds:(CGRect)bounds
{
    
    return CGRectInset(bounds, 20, 0);
//    CGRect inset = CGRectMake(bounds.origin.x+10, bounds.origin.y, bounds.size.width -10, bounds.size.height);//更好理解些
//    return inset;
}

@end
